﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RouterControl.Whs.Wizards;
using RouterControl.Whs.SingletonService;

namespace RouterControl.WhsAddIn.Wizards
{
    public partial class EditPortForwardingFinal : WizardPageClientBannerTop
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(EditPortForwardingFinal));
        private readonly SingletonService2Factory _service = new SingletonService2Factory();

        private EditPortForwarding ParentWizard2
        {
            get { return (EditPortForwarding)this.ParentWizard; }
        }

        public EditPortForwardingFinal()
        {
            InitializeComponent();
        }

        public override void StartRealWork()
        {
            base.StartRealWork();

            // load existing values
            var pf = this.ParentWizard2.Forwarding.Value;

            this.descTB.Text = pf.PortMappingDescription;
            this.enablePfCB.Checked = pf.Enabled;
            this.approvePfCB.Checked = true;
        }

        private void EditPortForwardingFinal_OkButtonClick(object sender, EventArgs e)
        {
            // make sure the description is not empty
            if (string.IsNullOrEmpty(this.descTB.Text))
            {
                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    Properties.Resources.txtPortForwardingWizNoDescriptionDesc,
                    Properties.Resources.txtPortForwardingWizNoDescriptionTitle,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );

                return;
            }

            // save values
            var fw = this.ParentWizard2.Forwarding.Value;

            fw.PortMappingDescription = this.descTB.Text;
            fw.Enabled = this.enablePfCB.Checked;

            this.ParentWizard2.Forwarding = fw;

            try
            {
	            // apply changes
	            // delete old port forwarding?
	            if (this.ParentWizard2.OriginalForwarding.HasValue &&
	                this.ParentWizard2.OriginalForwarding.Value.Id != fw.Id)
	            {
	                try
	                {
		                _service.Channel.DeletePortForwarding(this.ParentWizard2.OriginalForwarding.Value);
	                }
	                catch (Exception ex)
	                {
	                    _log.Error("Failed to delete obsolete port forwarding", ex);
	
	                    throw;
	                }
	            }
	
	            // add/update port forwarding
	            _service.Channel.AddOrUpdatePortForwarding(fw);

                // approve?
                if (this.approvePfCB.Checked)
                {
                    _service.Channel.SetPortForwardingApproval(fw, true);
                }
            }
            catch (Exception ex)
            {
                _log.Error("Failed to create/update port forwarding: " + fw.ToString(), ex);

                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    ex.Message,
                    Properties.Resources.txtPortForwardingWizApplyFailedTitle,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );

                return;
            }

            // done
            this.ParentWizard.DialogResult = DialogResult.OK;
            this.ParentWizard.Close();
        }

        private void EditPortForwardingFinal_BackButtonClick(object sender, EventArgs e)
        {
            this.SetCurrentPage<EditPortForwardingClient>();
        }
    }
}
